package com.example.myapplication.tack_24;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_24.data.OrderData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

public class ComplaintActivity extends AppCompatActivity {
    private ImageView exit;
    private Intent intent;
    private Gson gson;
    private OrderData.DataDTO orderData;
    private TextView submit;
    private JSONObject jsonObject;
    private EditText description;
    private TextView name;
    private TextView infoNo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_24_activity_complaint);
        init();
        submit_(orderData);
        exit_();
    }

    private void submit_(OrderData.DataDTO orderData) {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String s = description.getText().toString();
                    jsonObject.put("companyId",orderData.getCompanyId());
                    jsonObject.put("infoNo",orderData.getInfoNo());
                    jsonObject.put("questionType","1");
                    jsonObject.put("description",s);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String json = jsonObject.toString();
                getCom(json);

            }
        });

    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ComplaintActivity.this.finish();
            }
        });
    }

    private void getCom(String json) {
        String url = "prod-api/api/logistics-inquiry/logistics_complaint";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(ComplaintActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(ComplaintActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void init() {

        exit = findViewById(R.id.exit);
        name = findViewById(R.id.name);
        infoNo = findViewById(R.id.infoNo);
        submit = findViewById(R.id.submit);
        description = findViewById(R.id.description);
        intent = getIntent();
        String stringExtra = intent.getStringExtra(Common.COMMON);
        gson = new Gson();
        orderData = gson.fromJson(stringExtra, OrderData.DataDTO.class);
        name.setText("百世快递");
        infoNo.setText("ST0000001");
        jsonObject = new JSONObject();
    }
}